
proc setRotorLocal {az el} {
    setRotor $az $el
    #set reciprocalAz [expr abs(180 + $az)]
    #if {$reciprocalAz > 359} {
#	set reciprocalAz [expr $reciprocalAz -360]
 #   }
  #  set reciprocalEl [expr 180 - $el]
   # puts "reciprocal Az: $reciprocalAz"
   # puts "reciprocal El: $reciprocalEl"
   # setRotor $reciprocalAz $reciprocalEl
}
#Functions end

#main program enters

# DO NOT alter variables after this point

# The path to the directory containing AA2TX's 'ROTORDRV.EXE and 'ROTOR.EXE'
set iRotorDirectory "D://Documents and Settings/Bruce/Desktop/Rotordrv/"
set iRotorDr "${iRotorDirectory}ROTORDRV.EXE"
set iRotorExec "${iRotorDirectory}ROTOR.EXE"

# The parallel port to which the rotor controllers are connected
set rotorParallelPortNumber 1
# Is the azimuth stop at the south 'S' or north 'N'?
set stopPole "S"
# Should this program control the azimuth only 'AzOnly' or 'Both' rotors
set rotorMode "Both"

set flip "off"
source iRotorLib.tcl
# Check if RotorDRV is running, if not, start it with our parameters



startRotorDRV
testForRotorDRV
#Set tracking to 'on'
startiRotor



set az 0
set elev 0
set parkingTimeoutMinutes 10

# name of the serial port. Note the escaped slashes that must be used.
# Alter only the last five characters unless you really know what you're doing

set serialPort "\\\\.\\CNCB0"

# The speed of the serial connection, set in SatPC32's control. Use 57600
# unless there are difficulties with this.
set serialSpeed "57600"
#setupGui

# Open the serial port
set serial [open $serialPort r+]

# Configure the serial port
fconfigure $serial -mode "57600,n,8,1"

# With blocking set to '1' it seems a CTRL-C does not kill the command-line 
# program, so I'm using this approach
fconfigure $serial -blocking 0 -buffering none

# setup timer for parking timeout
set timeoutSeconds [expr $parkingTimeoutMinutes * 60]
#puts "timeoutseconds  $timeoutSeconds"
set timer [clock seconds]
set timeoutTime [expr $timer + $timeoutSeconds]
# The main loop to read from the serial input line



while {1} {
    # the following ensures that the while loop is not a major 
    # mouse wheel
    after 10
    variable timeoutTime
    variable az 
    variable elev
    variable input
    variable serial
    gets $serial input
    #puts $input
     # match a string of the form "W123 123", which is the sort of 
     # data the Yaesu driver gives
     # store the first three digits in $az and the last three in $elev
     # for further use.
    if {[regexp {W(\d)(\d)(\d) (\d)(\d)(\d)} $input output azone aztwo azthree elevone elevtwo elevthree]} {
	if {$azone == "0"} {
	    if {$aztwo == "0"} {
	    set az $azthree
	    } else {
		set az [expr $azthree + 10 * $aztwo]
	    }
	} else {
		set az [expr $azthree + 10 * $aztwo + 100 * $azone]
	    }
	
	   if {$elevone == "0"} {
	    if {$elevtwo == "0"} {
	    set elev $elevthree
	    } else {
		set elev [expr $elevthree + 10 * $elevtwo]
	    }
	   } else {
		set elev [expr $elevthree + 10 * $elevtwo + 100 * $elevone]
	    } 
	   
	
	puts "Input: Az: $az El: $elev"
       setRotorLocal $az $elev
    }
 }  
 if { ![info exists timeoutTime] } {
        set timeoutTime 0
    } else {
	incr timeoutTime 1
    }
    #puts "still in loop"
}

